package com.hrm.controller;

import com.github.pagehelper.PageInfo;
import com.hrm.mapper.DeptMapper;
import com.hrm.pojo.Dept;
import com.hrm.service.DeptService;
import com.hrm.utils.JwtUtil;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门模块的接口")
@CrossOrigin
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private JwtUtil jwtUtil;

//    @GetMapping("/list")
//    @ApiOperation(value = "获取部门列表")
//    public Result list(@ApiParam(required = true,value = "部门名称") String dept)throws Exception {
//        List<Dept> depts = deptService.findAll(dept);
//        if (depts == null || depts.isEmpty()) {
//            return new Result(false, StatusCode.ERROR, "查询部门列表失败");
//        }
//        return new Result(true, StatusCode.SUCCESS,"查询部门列表成功",depts);
//    }
    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表（无模糊查询）")
    public Result list()throws Exception {
        List<Dept> depts = deptService.findAll();
        if (depts == null || depts.isEmpty()) {
            return new Result(false, StatusCode.ERROR, "查询部门列表失败");
        }
        return new Result(true, StatusCode.SUCCESS,"查询部门列表成功",depts);
    }

    @PostMapping ("/page")
    @ApiOperation(value = "分页获取部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "deptName", value = "部门名称", required = false, paramType = "query", dataType = "String")
    })
    public Result page(Integer pageNum, Integer pageSize, String deptName) throws Exception {
        List<Dept> depts = deptService.findAllLikeName(pageNum, pageSize, deptName);
        if (depts == null || depts.isEmpty()) {
            return new Result(false, StatusCode.ERROR, "获取部门列表失败");
        }
        PageInfo<Dept> deptPageInfo = new PageInfo<>(depts, 10);
        return new Result(true, StatusCode.SUCCESS,"获取部门列表成功", deptPageInfo);
    }

    @PostMapping ("/add")
    @ApiOperation(value = "新增部门")
    @ApiImplicitParam(name = "dept", value = "部门实体类", required = true, paramType = "body", dataType = "Dept")
    public Result add(@RequestBody Dept dept) throws SQLException {
        if (deptMapper.selectDeptName(dept.getName())!= null){
            return new Result(false, StatusCode.ERROR, "部门名称已存在");
        }
        boolean b = deptService.addDept(dept);
        Result result = new Result();
        result.setFlag(b);
        result.setCode(b ? StatusCode.SUCCESS : StatusCode.ERROR);
        result.setMessage(b ? "部门添加成功" : "部门添加失败");
        result.setData(dept);
        return result;
    }
    @GetMapping("find")
    @ApiOperation(value = "根据id查询部门")
    @ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "query", dataType = "int")
    public Result findById(Integer id) {
        Dept dept = deptService.findById(id);
        Result result = new Result();
        result.setFlag(dept != null);
        result.setCode(dept != null ? StatusCode.SUCCESS : StatusCode.ERROR);
        result.setMessage(dept != null ? "部门查询成功" : "部门查询失败");
        result.setData(dept);
        return result;
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除部门")
    @ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "query", dataType = "int")
    public Result delete(@RequestBody ArrayList<Integer> deptIdList) throws Exception {
        boolean b = deptService.deleteDept(deptIdList);
        Result result = new Result();
        result.setFlag(b);
        result.setCode(b ? StatusCode.SUCCESS : StatusCode.ERROR);
        result.setMessage(b ? "部门删除成功" :"部门删除失败");
        return result;
    }
    @PutMapping("/update")
    @ApiOperation(value = "根据id更新部门")
    @ApiImplicitParam(name = "dept", value = "部门实体类", required = true, paramType = "body", dataType = "Dept")
    public Result update(@RequestBody Dept dept) {
        boolean b = deptService.updateDept(dept);
        Result result = new Result();
        result.setFlag(b);
        result.setCode(b ? StatusCode.SUCCESS : StatusCode.ERROR);
        result.setMessage(b ? "部门更新成功" : "部门更新失败");
        result.setData(dept);
        return result;
    }

}
